function SaveAnalysisHDF(hdfFilePath,name,value)

% SaveAnalysisHDF(hdfFilePath,name,value) saves data to an HDF file.
% hdfFilePath is a string containing the full. path to the file.
% name is a string containing the name of the variable.
% value is any HDF-valid variable containing the data to be saved (I'm not
% sure how far this list goes, but it contains integers, doubles, and
% arrays of either, the function automatically saves in the correct type if
% it can).
% If the variable already exists it will be overwritten.

fullDataPath = ['/dataAnalysis/' name];

% Check if dataset already exists, if it does, check sizes
% If not, create it.
% If it does exist, check sizes are commensurate.
% TODO: If not commensurate, resize?

try
    x = h5read(hdfFilePath,fullDataPath);
    if size(x) == size(value)
        h5write(hdfFilePath,fullDataPath,value)
    else
        warning('Data sizes non-commensurate, not saving (new data size doesn''t match old data size, so h5write() can''t overwrite data).')
        % HDF files need the new data to have the same size as the original
        % data, I haven't worked out how to resize them yet.
    end
catch err
    if strcmp(err.identifier,'MATLAB:imagesci:h5read:datasetDoesNotExist') || ...
            strcmp(err.identifier,'MATLAB:imagesci:h5read:libraryError')
        % Variable doesn't exist, make it:
        h5create(hdfFilePath,fullDataPath,size(value))
        h5write(hdfFilePath,fullDataPath,value)
    else
        rethrow(err);
    end
end